﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace NexTrackDesktop.ViewModels
{
    /// <summary>
    /// This class handles events in PartyActive view
    /// </summary>
    class PartyActiveViewModel : ViewModelBase
    {
        public ObservableCollection<QueueItemViewModel> QueueItems { get; private set; }
        public SongViewModel NowPlaying
        {
            get;
            private set;
        }

        public ICommand SkipCommand { get; private set; }

        public bool PlayPauseState
        {
            get { return Client.Player.Playing; }
            set 
            {
                if (value) 
                    Client.Player.Resume();
                else 
                    Client.Player.Pause();
            }
        }

        /// <summary>
        /// Creates ViewModel and links user input to commands
        /// </summary>
        public PartyActiveViewModel()
        {
            QueueItems = new ObservableCollection<QueueItemViewModel>();

            NowPlaying = new SongViewModel();
            SkipCommand = new DelegateCommand(Client.Party.NextSong, () => true);
            Client.Player.PropertyChanged += Player_PropertyChanged;
            Client.Party.Queue.QueueUpdated += Queue_QueueUpdated;
        }

        /// <summary>
        /// This method updates queue to be displayed on PartyActive view
        /// </summary>
        void Queue_QueueUpdated()
        {
            QueueItems.UpdateInPlace(Client.Party.Queue.Items, (qvm, qi) => { qvm.Update(qi); });
        }

        /// <summary>
        /// This method updates NexTrackPlayer view
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Player_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            NowPlaying.Song = Client.Player.NowPlaying;
            NotifyPropertyChanged(() => PlayPauseState);
        }
    }
}
